import UserModel from "@src/models/User";
import { UserResponse } from '@src/libcommon/modules/user'
import { resExample } from "./common/responseData";
import { IResponse } from "@src/libcommon";
import HttpStatusCodes from "@src/common/HttpStatusCodes";
import md5 from "md5";

class UserService {
    /**
     * 获取用户信息
     * @param {number} userId 用户ID
     * @return {Promise<UserResponse | resExamle>}
     */
    getUsers = async function (userId: number): Promise<UserResponse | IResponse> {
        try {
            const model = await UserModel()
            const user = await model.findOne({
                where: {
                    id: userId
                },
                attributes: ['username', 'avatar', 'email', 'signature']
            })
            if (!user) {
                return resExample({
                    code: HttpStatusCodes.NOT_FOUND,
                    message: '用户不存在',
                    success: false,
                    data: null
                })
            }
            return resExample({
                code: HttpStatusCodes.OK,
                message: '获取用户信息成功',
                success: true,
                data: user
            })
        } catch (error) {
            throw resExample({
                code: HttpStatusCodes.INTERNAL_SERVER_ERROR,
                message: '获取用户信息失败',
                success: false,
                data: null
            })
        }
    }
    /**
     * 更新用户信息
     * @param {number} userId 用户ID
     * @param {object} data 用户信息
     * @return {Promise<resExamle>}
     */
    updateUser = async function (userId: number, data: object): Promise<IResponse> {
        try {
            const model = await UserModel()
            const user = await model.update(data, {
                where: {
                    id: userId
                }
            })
            if (!user) {
                return resExample({
                    code: HttpStatusCodes.NOT_FOUND,
                    message: '用户不存在',
                    success: false,
                    data: null
                })
            }
            return resExample({
                code: HttpStatusCodes.OK,
                message: '更新用户信息成功',
                success: true,
                data: null
            })
        } catch (error) {
            throw resExample({
                code: HttpStatusCodes.INTERNAL_SERVER_ERROR,
                message: '更新用户信息失败',
                success: false,
                data: null
            }
            )
        }
    }
    /**
     * 修改用户密码
     * @param {number} userId 用户ID
     * @param {any} data 用户信息
     * @return {Promise<resExamle>}
     */
    updatePassword = async function (userId: number, data: any): Promise<IResponse> {
        try {
            const model = await UserModel()

            const { password, email } = data

            // 验证邮箱是否是该用户的绑定邮箱
            const user = await model.findOne({
                where: {
                    id: userId,
                    email
                }
            })
            if (!user) {
                return resExample({
                    code: HttpStatusCodes.NOT_FOUND,
                    message: '邮箱错误',
                    success: false,
                    data: null
                })
            }
            // 更新密码
            const user2 = await model.update({
                password: md5(password)
            }, {
                where: {
                    id: userId
                }
            })
            if (!user2) {
                return resExample({
                    code: HttpStatusCodes.NOT_FOUND,
                    message: '用户不存在',
                    success: false,
                    data: null
                })
            }

            return resExample({
                code: HttpStatusCodes.OK,
                message: '修改用户密码成功',
                success: true,
                data: null
            })
        } catch (error) {
            throw resExample({
                code: HttpStatusCodes.INTERNAL_SERVER_ERROR,
                message: '修改用户密码失败',
                success: false,
                data: null
            })
        }
    }
}

export default new UserService();